﻿@{
    ViewData["Title"] = "AddWarehouse";
    Layout = "~/Views/Shared/Template.cshtml";
}
<h4>新增仓库</h4>
<!-- 给容器追加 class="layui-form-pane"，即可显示为方框风格 -->
<form class="layui-form" action="/CreateWarehouse" method="post">
    <div class="layui-form-item layui-row">
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">仓库编号:</label>
            <div class="layui-input-block">
                <input type="text" name="warehouseNo" id="warehouseNo" lay-verify="required" placeholder="可不填写，自动生成" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">部门:</label>
            <div class="layui-input-block">
                <select id="departmentId" name="departmentId" lay-verify="required">
                    <option value="">请选择</option>
                    <option value="1">财务中心</option>
                    <option value="2">信息技术中心</option>
                    <option value="3">采购中心</option>
                    <option value="4">销售中心</option>
                    <option value="5">行政部</option>
                </select>
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-row">
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">仓库名称:</label>
            <div class="layui-input-block">
                <input type="text" id="warehouseName" name="warehouseName" required lay-verify="required" placeholder="请输入仓库名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">仓库类型:</label>
            <div class="layui-input-block">
                <select name="warehouseTypeId" id="warehouseTypeId" lay-filter="aihao" lay-verify="required"></select>
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-row">
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">作用:</label>
            <div class="layui-input-block">
                <input type="text" name="function" id="function" placeholder="请输入仓库作用" autocomplete="off" class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">租赁时间:</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="date" name="leaseDate" id="leaseDate" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-row">
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">面积:</label>
            <div class="layui-input-block">
                <input type="text" name="warehouseArea" id="warehouseArea" placeholder="请填写面积" autocomplete="off" class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">地址:</label>
            <div class="layui-input-block">
                <input type="text" name="warehouseAddress" id="warehouseAddress" placeholder="请输入仓库地址" autocomplete="off" class="layui-input" lay-verify="required">
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-row">
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">联系人:</label>
            <div class="layui-input-block">
                <input type="text" name="warehouseContact" id="warehouseContact" placeholder="请填写联系人" autocomplete="off" class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">电话:</label>
            <div class="layui-input-block">
                <input type="text" name="warehousePhone" id="warehousePhone" placeholder="请填写电话" autocomplete="off" class="layui-input" lay-verify="required">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">取消</button>
        </div>
    </div>
</form>
<script>
    //文档就绪函数
    $(function () {
        generateDate();
    });
    //仓库类型下拉框
    layui.use('form', function () {
        var form = layui.form;
        $.ajax({
            url: "/Warehouse/GetWarehouseTypeShow",
            type: "get",
            data: { token: localStorage.getItem("token") },
            success: function (data) {
                var data = JSON.parse(data);
                console.log(data.data)
                var options = '<option value="">请选择</option>'; // 添加默认选项
                $.each(data.data, function (index, item) {
                    options += '<option value="' + item.warehouseTypeId + '">' + item.warehouseTypeName + '</option>';
                });
                $("#warehouseTypeId").html(options);
                form.render('select'); // 重新渲染下拉框
            },
            error: function () {
                console.log("获取数据失败");
                // 可以考虑显示一个错误消息给用户
            }
        });
    });
    //自动生成时间
    function generateDate() {
        var n = new Date();
        var ss = n.getFullYear() + (n.getMonth() + 1).toString().padStart(2, '0') +
            n.getDate().toString().padStart(2, '0') +
            n.getHours().toString().padStart(2, '0') +
            n.getMinutes().toString().padStart(2, '0') +
            n.getSeconds().toString().padStart(2, '0');
        // var ass = Math.floor(Math.random() * 900 + 100);
        // $("#warehouseNo").val(ss + '-' + ass);
        $("#warehouseNo").val(ss);
    }

   
    layui.use(['layer', 'form', 'util'], function () {
        var layer = layui.layer;
        // var $ = layui.layer;
        var form = layui.form;
        var util = layui.util;

        // 表单提交事件
        form.on('submit(formDemo)', function (data) {
            var field = data.field; // 获取表单字段值

            // 此处可执行 Ajax 等操作
            $.ajax({
                url: '/Warehouse/GetAddWarehouse',
                type: 'post',
                data: {
                    field: field,
                    token: localStorage.getItem("token"),
                },
                success: function (res) {
                    var obj = JSON.parse(res);
                    console.log(field);
                    console.log(obj);
                    if (obj.code == 1) {
                        layer.alert("添加成功");
                        // 可在此处添加刷新页面或重置表单等操作，例如：
                        // form.resetField();
                        // location.reload();
                        //跳转页面
                        location.href = '/Warehouse/WarehouseShow'
                    } else {
                        layer.alert("添加失败");
                    }
                }
            });

            return false; // 阻止默认 form 跳转
        });

        // 对弹层中的表单进行初始化渲染
        form.render();
    });
</script>

